package eyrolles;

import java.util.Properties;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;

import javax.servlet.ServletException;
import javax.servlet.ServletContext;
import javax.sql.DataSource;

import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.action.ActionServlet;

public class EyrollesPlugin implements PlugIn {

    public static final String PROPERTIES = "PROPERTIES";
    private String filePath = null;
    private String configModel = null;

    public void init(ActionServlet servlet, ModuleConfig applicationConfig)
            throws javax.servlet.ServletException {

        EyrollesModele modele = null;
        ServletContext context = servlet.getServletContext();
        Properties properties = new Properties();

        try {
            File file = new File(filePath);
            FileInputStream fis;
            fis = new FileInputStream(file);
            properties.load(fis);
            file = new File(configModel);
            fis = new FileInputStream(file);
            properties.load(fis);
            modele = (EyrollesModele) Factory.instantiates(properties.getProperty("modelClass"));
        } catch (ClassNotFoundException ex) {
            throw new ServletException(ex.getMessage());
        } catch (InstantiationException ex) {
            throw new ServletException(ex.getMessage());
        } catch (IllegalAccessException ex) {
            throw new ServletException(ex.getMessage());
        } catch (FileNotFoundException ex) {
            throw new ServletException(ex.getMessage());
        } catch (IOException ioe) {
            throw new ServletException(ioe.getMessage());
        } catch (Exception e) {
            throw new ServletException(e.getMessage());
        }
        
        modele.setProperties(properties);
        context.setAttribute("modele", modele);
    }

    public String getConfigModel() {
        return this.configModel;
    }

    public void setConfigModel(String s) {
        this.configModel = s;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String f) {
        filePath = f;
    }

    public void destroy() {
    }
}
